{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 循环神经网络"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "import torch.nn"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "循环神经元"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "weight_ih = Parameter containing:\n",
      "tensor([[ 0.1546, -0.4201,  0.3069],\n",
      "        [-0.2943, -0.4141,  0.3779],\n",
      "        [-0.1165,  0.4225,  0.2909],\n",
      "        [-0.2210,  0.1744,  0.0445],\n",
      "        [-0.0775, -0.0825,  0.2698],\n",
      "        [-0.4021,  0.1798, -0.1067],\n",
      "        [ 0.2447,  0.1739,  0.3121],\n",
      "        [ 0.3183,  0.4011,  0.4168],\n",
      "        [ 0.2450,  0.2716, -0.2862],\n",
      "        [-0.3181,  0.0293,  0.3267],\n",
      "        [ 0.0849,  0.2340, -0.1914],\n",
      "        [ 0.3657, -0.3483,  0.2243],\n",
      "        [-0.3867,  0.0610,  0.1920],\n",
      "        [ 0.2874, -0.1962, -0.1405],\n",
      "        [ 0.3990, -0.3319, -0.3178],\n",
      "        [-0.2392,  0.0449, -0.0476],\n",
      "        [ 0.4250,  0.2782,  0.1471],\n",
      "        [ 0.3917,  0.1959, -0.1446],\n",
      "        [ 0.4112, -0.3560, -0.4284],\n",
      "        [ 0.2853,  0.1100,  0.2023]])\n",
      "weight_hh = Parameter containing:\n",
      "tensor([[-0.3648,  0.4430, -0.3348,  0.4109, -0.1680],\n",
      "        [ 0.1527, -0.2936, -0.2579, -0.4133,  0.3711],\n",
      "        [ 0.1496,  0.0196,  0.3598, -0.0696, -0.3412],\n",
      "        [-0.2432, -0.3334,  0.0562, -0.2669,  0.3556],\n",
      "        [-0.1910, -0.3552, -0.0925, -0.4088, -0.3444],\n",
      "        [-0.3098, -0.0721, -0.3810,  0.0838, -0.0609],\n",
      "        [-0.2973, -0.3589, -0.1609, -0.3379, -0.0488],\n",
      "        [-0.1887, -0.2095, -0.2041,  0.2143,  0.2813],\n",
      "        [ 0.1846, -0.1316, -0.3792, -0.0299,  0.3355],\n",
      "        [ 0.4467, -0.4216,  0.4261,  0.3072,  0.0398],\n",
      "        [ 0.2752, -0.2967, -0.1792,  0.0286,  0.2560],\n",
      "        [-0.2974, -0.2326, -0.3057,  0.0801, -0.3294],\n",
      "        [-0.0940,  0.2447, -0.0433,  0.2762, -0.0265],\n",
      "        [ 0.1286, -0.2445,  0.0341,  0.1010,  0.2282],\n",
      "        [ 0.2498, -0.0942,  0.1513, -0.0724, -0.3092],\n",
      "        [-0.2541, -0.0784, -0.2867, -0.1284, -0.2805],\n",
      "        [ 0.1271,  0.2362,  0.1033, -0.1615,  0.0383],\n",
      "        [-0.0746,  0.0173, -0.1201,  0.2438, -0.2124],\n",
      "        [-0.0743, -0.3551, -0.1041,  0.1748, -0.0314],\n",
      "        [ 0.3987,  0.4226, -0.2135,  0.1398,  0.4012]])\n",
      "bias_ih = Parameter containing:\n",
      "tensor([-0.1281,  0.2316,  0.4288, -0.2878, -0.0910,  0.3655, -0.1387,\n",
      "         0.2566,  0.2411,  0.0738, -0.2835, -0.1071, -0.3023, -0.3748,\n",
      "         0.0079,  0.3198, -0.1465, -0.3762, -0.2246, -0.1073])\n",
      "bias_hh = Parameter containing:\n",
      "tensor([ 0.4274, -0.2398,  0.0142, -0.2030, -0.3743, -0.2485, -0.0872,\n",
      "         0.2738, -0.2807,  0.0631,  0.3901,  0.1708, -0.4178,  0.0804,\n",
      "        -0.1740, -0.0407,  0.0563, -0.1466,  0.2019, -0.0861])\n"
     ]
    }
   ],
   "source": [
    "cell = torch.nn.LSTMCell(input_size=3, hidden_size=5)\n",
    "for name, param in cell.named_parameters():\n",
    "    print ('{} = {}'.format(name, param))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[ 0.0732,  0.1581,  0.3137, -0.2106, -0.1092],\n",
       "         [ 0.1603, -0.2076, -0.1196, -0.1444, -0.1216]],\n",
       "\n",
       "        [[ 0.2538, -0.0410,  0.0857, -0.2496, -0.0189],\n",
       "         [ 0.1475, -0.1631, -0.3240, -0.1581, -0.0433]],\n",
       "\n",
       "        [[ 0.3371, -0.0973, -0.1303, -0.2868, -0.0598],\n",
       "         [ 0.1204,  0.2496, -0.0261, -0.0944,  0.1428]],\n",
       "\n",
       "        [[ 0.1067,  0.2162, -0.0122, -0.0719,  0.1257],\n",
       "         [ 0.2920, -0.0272, -0.2666, -0.2972,  0.1752]],\n",
       "\n",
       "        [[ 0.1566,  0.1770, -0.1212, -0.1658,  0.1794],\n",
       "         [ 0.2071,  0.0663, -0.1071, -0.1625,  0.1738]],\n",
       "\n",
       "        [[ 0.1330,  0.0043, -0.2234, -0.1526,  0.1552],\n",
       "         [ 0.1273,  0.2843, -0.0382, -0.1195,  0.1782]]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "seq_len, batch_size = 6, 2\n",
    "input_size, hidden_size = 3, 5\n",
    "cell = torch.nn.LSTMCell(input_size=input_size, hidden_size=hidden_size)\n",
    "inputs = torch.randn(seq_len, batch_size, input_size)\n",
    "h = torch.randn(batch_size, hidden_size)\n",
    "c = torch.randn(batch_size, hidden_size)\n",
    "hs = []\n",
    "for t in range(seq_len):\n",
    "    h, c = cell(inputs[t], (h, c))\n",
    "    hs.append(h)\n",
    "outputs = torch.stack(hs)\n",
    "outputs"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "循环神经网络"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[ 0.3042, -0.0433,  0.2862, -0.0866,  0.0233],\n",
       "         [ 1.1850, -0.3314,  0.3698,  0.0435,  0.3782]],\n",
       "\n",
       "        [[ 0.5028,  0.0474,  0.4993, -0.0569, -0.1327],\n",
       "         [ 0.8510, -0.2051,  0.1971, -0.1291,  0.1303]],\n",
       "\n",
       "        [[ 0.5346,  0.1130,  0.5334, -0.0580, -0.1849],\n",
       "         [ 0.7298, -0.0204,  0.2554, -0.3126, -0.0835]],\n",
       "\n",
       "        [[ 0.4235,  0.1390,  0.4812,  0.0389, -0.1578],\n",
       "         [ 0.5358,  0.0757,  0.3022, -0.2219, -0.1193]],\n",
       "\n",
       "        [[ 0.3670,  0.1709,  0.4674,  0.0571, -0.1334],\n",
       "         [ 0.4393,  0.1196,  0.3984, -0.1327, -0.1680]],\n",
       "\n",
       "        [[ 0.3976,  0.2156,  0.4842, -0.0041, -0.1286],\n",
       "         [ 0.4780,  0.1968,  0.4448, -0.1736, -0.1918]]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "num_layers = 2\n",
    "seq_len, batch_size = 6, 2\n",
    "input_size, hidden_size = 3, 5\n",
    "rnn = torch.nn.GRU(input_size, hidden_size, num_layers=num_layers)\n",
    "inputs = torch.randn(seq_len, batch_size, input_size)\n",
    "h0 = torch.randn(num_layers, batch_size, hidden_size)\n",
    "outputs, hn = rnn(inputs, h0)\n",
    "outputs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Parameter containing:\n",
      "tensor([[ 0.5751, -0.2216,  0.2251,  0.0647,  0.2716,  0.1060, -0.0162,\n",
      "          0.0417, -0.3813,  0.3719],\n",
      "        [-0.3064, -0.1623, -0.2981,  0.0982, -0.1122,  0.0357, -0.3964,\n",
      "          0.5385,  0.0034, -0.2868],\n",
      "        [ 0.1982, -0.0780, -0.4070, -0.3685, -0.4458, -0.3976,  0.3372,\n",
      "          0.3086, -0.3340, -0.0211]])\n",
      "Parameter containing:\n",
      "tensor([[-0.5260, -0.1456,  0.3338],\n",
      "        [-0.0070, -0.2073, -0.5632],\n",
      "        [ 0.1125,  0.5314,  0.2881]])\n",
      "Parameter containing:\n",
      "tensor([ 0.1285, -0.1500,  0.4653])\n",
      "Parameter containing:\n",
      "tensor([ 0.4319,  0.3306, -0.0562])\n",
      "Parameter containing:\n",
      "tensor([[-0.2565, -0.0548,  0.0921, -0.2308, -0.3747,  0.0458, -0.5546,\n",
      "          0.0367, -0.1622,  0.3625],\n",
      "        [-0.0949, -0.1870,  0.2820,  0.3193, -0.4838,  0.3729,  0.3045,\n",
      "         -0.2570,  0.4971, -0.0430],\n",
      "        [ 0.5405,  0.2729,  0.2505,  0.2948, -0.3279, -0.3744, -0.1306,\n",
      "         -0.0003,  0.3529, -0.2866]])\n",
      "Parameter containing:\n",
      "tensor([[-0.4026,  0.4768,  0.0259],\n",
      "        [-0.5224,  0.1138, -0.2645],\n",
      "        [ 0.1981,  0.1033,  0.3735]])\n",
      "Parameter containing:\n",
      "tensor([ 0.0300, -0.5495, -0.5014])\n",
      "Parameter containing:\n",
      "tensor([ 0.2162,  0.1487, -0.5498])\n",
      "Parameter containing:\n",
      "tensor([[-0.4899,  0.4171, -0.0911,  0.3250, -0.5180, -0.3922],\n",
      "        [-0.3123,  0.2174, -0.2666,  0.3429, -0.2434,  0.3834],\n",
      "        [ 0.3729, -0.3259,  0.1132, -0.3572,  0.1931,  0.3238]])\n",
      "Parameter containing:\n",
      "tensor([[ 0.1561,  0.1683,  0.4404],\n",
      "        [ 0.3944,  0.2305,  0.5667],\n",
      "        [-0.1011, -0.3561, -0.4035]])\n",
      "Parameter containing:\n",
      "tensor([ 0.2449, -0.5458,  0.5738])\n",
      "Parameter containing:\n",
      "tensor([ 0.5705,  0.5695,  0.2943])\n",
      "Parameter containing:\n",
      "tensor([[ 0.1447, -0.5643,  0.1390,  0.1262, -0.4101,  0.5553],\n",
      "        [-0.0792, -0.2701, -0.1528, -0.3426,  0.4866,  0.0890],\n",
      "        [ 0.2180, -0.5656,  0.2154, -0.3164, -0.2413, -0.5382]])\n",
      "Parameter containing:\n",
      "tensor([[-0.1591,  0.1375, -0.1221],\n",
      "        [ 0.1201,  0.5025,  0.1780],\n",
      "        [ 0.1092,  0.2713,  0.5039]])\n",
      "Parameter containing:\n",
      "tensor([ 0.5634,  0.2071, -0.2185])\n",
      "Parameter containing:\n",
      "tensor([ 0.5445,  0.2642,  0.3671])\n"
     ]
    }
   ],
   "source": [
    "num_layers = 2\n",
    "hidden_size = 3\n",
    "bidirectional = True\n",
    "rnn = torch.nn.RNN(input_size=10, hidden_size=hidden_size, num_layers=num_layers, bidirectional=bidirectional)\n",
    "inputs = torch.randn(5, 3, 10)\n",
    "h0 = torch.randn(num_layers * 2 if bidirectional else num_layers, 3, hidden_size)\n",
    "outputs, hn = rnn(inputs, h0)\n",
    "for param in rnn.parameters():\n",
    "    print (param)\n",
    "    # print (param.size())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
